package com.example.jg;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import com.example.jg.bean.NaviBean;
import com.example.jg.not.ApiServcie;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class NaviActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navi);
        new Thread(new Runnable() {
            @Override
            public void run() {
                Request build1 = new Request.Builder()
                        .url("https://wanandroid.com/navi/json")
                        .build();
                OkHttpClient okHttpClient = new OkHttpClient();
                okhttp3.Call call = okHttpClient.newCall(build1);
                call.enqueue(new okhttp3.Callback() {
                    @Override
                    public void onFailure(okhttp3.Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(okhttp3.Call call, okhttp3.Response response) throws IOException {
                        String body = response.body().toString();
                        try {
                            JSONObject jsonObject = new JSONObject(body);
                            JSONArray data = jsonObject.getJSONArray("data");
                            for (int i = 0; i < data.length(); i++) {
                                JSONArray articles = data.getJSONObject(i).getJSONArray("articles");
                                for (int j = 0; j < articles.length(); j++) {
                                    String author = articles.getJSONObject(i).getString("author");
                                    String niceShareDate = articles.getJSONObject(i).getString("niceShareDate");
                                    String title = articles.getJSONObject(i).getString("title");
                                    String publishTime = articles.getJSONObject(i).getString("publishTime");
                                    String niceDate = articles.getJSONObject(i).getString("niceDate");
                                    boolean collect = articles.getJSONObject(i).getBoolean("collect");
                                    String courseId = articles.getJSONObject(i).getString("courseId");
                                    Log.i("aaa", "onResponse: "+author+niceDate+title+publishTime);
                                }
                            }
                        } catch (JSONException e) {

                        }
                    }
                });
            }
        }).start();
        //jsoup
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Document doc = (Document) Jsoup.connect("http://192.168.0.195:8088/news.html").get();//解析html
                    Elements links = doc.select("ul[class=w_newslistpage_list]");//获取li标签且class为w_newslistpage_list的标签
                    for (Element link : links) {
                        Elements li = link.select("li");//查找li标签
                        for (Element element : li) {//遍历
                            Elements select = element.select("a[title]");//查找a标签且带有title属性的标签
                            if (select!=null&&select.size()>0){
                                String linkHref = select.get(0).attr("href");//获取href值
                                String linkText = select.get(0).text();//获取text
                                System.out.println("爬虫结果 1 -->  " + linkHref +linkText);
                            }
                            Elements select1 = link.select("span[class=date]");//获取span标签且class为date的标签
                            if (select1!=null&&select1.size()>0){
                                String date = select1.get(0).text();
                                System.out.println("爬虫结果 2--> " + date);
                            }
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }
}
